
package de.uni.wuppertal.server.ui.support;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.*;

import de.uni.wuppertal.server.ServerInfo;
import de.uni.wuppertal.server.ServerManager;
import de.uni.wuppertal.server.util.Resource;

/**
 * @author Martin Schröder
 * @author Patrik Nordsiek
 */
public class ServerListCellRenderer extends DefaultListCellRenderer
{
	private static final long serialVersionUID = 1L;
	
	private static final ImageIcon ICON_ONLINE = Resource.getIcon("icon/online.png");
	private static final ImageIcon ICON_OFFLINE = Resource.getIcon("icon/offline.png");
	
	private ServerManager manager;
	
	public ServerListCellRenderer(ServerManager manager)
	{
		super();
		
		this.manager = manager;
	}
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus)
	{
		if(value instanceof ServerInfo)
		{
			ServerInfo server = (ServerInfo)value;
			
			JPanel panel = new JPanel(new BorderLayout());
			panel.setBorder(BorderFactory.createEmptyBorder(1, 3, 1, 3));
			panel.setOpaque(false);
			
			JLabel label = (JLabel)super.getListCellRendererComponent(list, server.getName(), index, isSelected, hasFocus);
			label.setIcon(manager.isRunning(server) ? ICON_ONLINE : ICON_OFFLINE);
			
			panel.add(label, BorderLayout.CENTER);
			
			return panel;
		}
		
		return new JLabel(value.toString());
	}
}
